<!--
 * @description  : 市管-关联企业
 * @descriptionDetail: 市管-关联企业
 * @copyright    : 浙江烟草
 * @author       : sy
 * @create       : 2023-06-29 15:43:02
-->
<template>
  <view class="page">
    <!-- #ifdef H5-ONLY -->
    <theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999">
    </theme-navbar>
    <!-- #endif -->
    <view class="content u-page">
      <u-form
        :error-type="errorType"
        class="fpad"
        :model="relRetailer"
        :label-width="240"
        label-position="left"
        ref="uForm"
      >
        <u-form-item :required="true" label="企业名称" prop="entName">
          <u-input type="select" input-align="right" :border="false" placeholder="请选择企业名称" v-model="relRetailer.entName" @click="addRelRetail" />
        </u-form-item>
        <u-form-item label="企业所在地">
          <u-input type="text" input-align="right" :disabled="true" :border="false" placeholder="根据选择的无证户/持证户信息自动写入" v-model="relRetailer.entAddress" />
        </u-form-item>
        <u-form-item label="负责人">
          <u-input type="text" input-align="right" :disabled="true" :border="false" placeholder="根据选择的无证户/持证户信息自动写入" v-model="relRetailer.managerName" />
        </u-form-item>
      </u-form>
    </view>
    <view class="submit-btns btn-other-cls" v-if="!isDisabled">
      <u-button class="u-btn" type="primary" @click="confirm">
        保存
      </u-button>
    </view>
  </view>
</template>

<script>
import checkService from "@/service/check/check.service";
export default {
  onLoad(opt) {
    this.params = this.getRouteParam();
    if(this.params && this.params.param){	
      this.getRelRetailer(this.params.param.relUuid);
    }
    this.isDisabled = this.params.type == 'view' ? true : false;
  },
  data() {
    return {
      errorType: ['border-bottom',"message",],
      relRetailer: {},
      rules: {
        entName: [{
          required: true,
          message: '请选择企业名称',
          trigger: ['blur', 'change',],
        },],
      },
      isDisabled:false,
      params:{},
    }
  },
  mounted() {
  },
  onReady() {
    this.$refs.uForm.setRules(this.rules);
  },
  methods: {

    // 获取关联企业详情
    getRelRetailer(id){
      this.$u.loading('加载中...');
      checkService.findEntById({id:id,}).then((rs) => {
        this.$u.hideLoading();
        if(rs.success){
          this.relRetailer = rs.bean;
        }else{
          this.$u.toast('查询详情失败！');
        }
      }).catch((err) => {
        this.$u.toast(err);
        this.$u.hideLoading();
      })
    },
    // 选择关联企业
    addRelRetail(){
      if(!this.relRetailer.relUuid){
        this.$u.route("/pages/check/undocuEcigarette/relEntListSelect?&isSingle=1&isRelRetailer=1&title=选择关联企业&entUuid=" + this.params.entUuid);
      } 
    },
    updateBackState(data) {
      if(data['relEntList']){
        let rel = data['relEntList'];
        this.relRetailer = rel.length > 0 ? rel[0] : {};
      }
    },
    confirm() {
      let self = this;
      this.$refs.uForm.validate(valid => {
        if (valid) {
          checkService.saveOrUpdateEnt({ownerEntUuid:this.params.entUuid,relEntUuid:this.relRetailer.entUuid}).then((rs) => {
            if(rs.success){
              self.backPrev({
                paramKey: "relEnt",
                param: rs.bean,
              });  
            }else{
              this.$u.toast(rs.msg);
            }
          }).catch((err) => {
            this.$u.toast(err);
          });
        }
      });
    },
  },
}
</script>

<style scoped lang="scss">
.page{
  padding: 0;
}
.right-icon {
  padding: 0 25rpx;
  font-size: 30rpx;
  color: white;
}
.fpad {
  padding: 20rpx 30rpx;
}
</style>
